package com.uphone.kingmall.utils.web;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.utils.navigationbar.DefaultNavigationBar;

import java.util.Map;

/**
 * 带webview 的 Activity 基类
 * 没有重写返回键，有需要的自行重写
 */
public class BaseWebActivity extends BaseActivity {

    public ProgressWebView mWebview;
    public String url;
    public String title;

    private Map<String, String> extraHeaders; // 添加请求头
    private LinearLayout ll;

    public void reload() {
        LogUtils.e("刷新页面");
        if (mWebview != null)
            mWebview.loadUrl(url);
    }

    /**
     * 处理数据
     */
    private void ToDealWithData() {
        if (TextUtils.isEmpty(title)) {
            title = "王者商城";
        }
    }

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_base_activity_web;
    }

    @Override
    public void initView() {
        try {
            Intent intent = getIntent();
            title = intent.getStringExtra("title");// 标题
            url = intent.getStringExtra("url"); // 网址

            String action = intent.getAction();
            if (Intent.ACTION_VIEW.equals(action)) {
                Uri uri = intent.getData();
                if (uri != null) {
                    title = uri.getQueryParameter("title");
                    url = uri.getQueryParameter("url");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mWebview = findViewById(R.id.wv_base_webview);
        ll = findViewById(R.id.ll);
        if (TextUtils.isEmpty(url)) {
            url = getUrl();
        }
        if (!TextUtils.isEmpty(title)) {
            new DefaultNavigationBar.Builder(this,ll).setTitle(title).builder();
        }
        ToDealWithData();
        initWebView();
    }

    @Override
    public void initData() {

    }

    public void initWebView() {
        initWebView(mWebview);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView(ProgressWebView wv) {
        WebSettings mWebSettings = wv.getSettings();
        //加载缓存否则网络
        mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //图片自动缩放 打开
        mWebSettings.setLoadsImagesAutomatically(true);
        //硬件解码
        wv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        // 设置支持javascript脚本
        mWebSettings.setJavaScriptEnabled(true);
        // 设置可以支持缩放
        mWebSettings.setSupportZoom(true);
        // 设置出现缩放工具 是否使用WebView内置的缩放组件，由浮动在窗口上的缩放控制和手势缩放控制组成，默认false
        mWebSettings.setBuiltInZoomControls(true);
        //隐藏缩放工具
        mWebSettings.setDisplayZoomControls(false);
        // 扩大比例的缩放
        mWebSettings.setUseWideViewPort(true);
        //自适应屏幕
        mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setDatabaseEnabled(true);
        //是否开启本地DOM存储  鉴于它的安全特性（任何人都能读取到它，尽管有相应的限制，将敏感数据存储在这里依然不是明智之举），Android 默认是关闭该功能的。
        mWebSettings.setDomStorageEnabled(true);
        wv.setSaveEnabled(true);
        wv.setKeepScreenOn(true);
        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view.loadUrl(request.getUrl().toString());
                }
                return true;
            }
        });
        //加载指定网页
        if (url.startsWith("http")) {
            wv.loadUrl(url);
        } else {
            wv.loadDataWithBaseURL(null, url, "text/html", "utf-8", null);
        }
        wv.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebview.destroy();
    }

    public String getUrl() {
        return url;
    }

}